package cn.tx.demo;

import java.sql.*;

public class JDBCDemo4 {

    static  final  String username="root";
    static  final  String password="root";
    static  final  String url="jdbc:mysql://127.0.0.1:3306/mydb4";
    static  final  String driver="com.mysql.jdbc.Driver";

    public static void insert(){
        String sql="insert into emp( EMP_ID, ENAME , GENDER ,HIREDATE) values(null,?,?,?);";
        

        Connection conn=null;
        PreparedStatement stmt=null;
        try {
            //驱动加载
            Class.forName(driver);
            //建立连接
            conn = DriverManager.getConnection(url, username, password);
            //获得一个执行SQL的对象
            stmt= conn.prepareStatement(sql);
            stmt.setString(1,"亮哥");
            stmt.setInt(2,1);
            stmt.setDate(3,new java.sql.Date(System.currentTimeMillis()));

            //执行SQL
            int i = stmt.executeUpdate();
            System.out.println("插入完毕"+i+"条");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
    public static void update(){
        String sql="update emp t set t.ename=?,t.gender=?,t.hiredate=? where t.emp_id=?";


        Connection conn=null;
        PreparedStatement stmt=null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);

            stmt= conn.prepareStatement(sql);
            stmt.setString(1,"龙哥");
            stmt.setInt(2,1);
            stmt.setDate(3,new java.sql.Date(System.currentTimeMillis()));
            stmt.setInt(4,5);

            stmt.executeUpdate();
            System.out.println("执行完毕");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
    public static void delete(){
        String sql="delete from emp where emp_id=2;";


        Connection conn=null;
        Statement stmt=null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);

            stmt= conn.createStatement();
            stmt.executeUpdate(sql);
            System.out.println("执行完毕");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
    public static void select(){
        String sql="select * from emp ";


        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try {
            //驱动加载
            Class.forName(driver);
            //建立连接
            conn = DriverManager.getConnection(url, username, password);
            //获得一个执行SQL的对象
            stmt= conn.createStatement();
            //执行SQL
             rs= stmt.executeQuery(sql);
             while(rs.next()){
                 int empId = rs.getInt(1);
                 String ename = rs.getString(2);
                 String gender = rs.getString(3);
                 Date date = rs.getDate(4);

                 System.out.println("empid:"+empId+"  ename:"+ename+"  gender:"+gender+"  date:"+date);


             }

            System.out.println("执行完毕");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


    public static void main(String[] args) {

        update();
    }
}
